package ar.edu.unicen.exa.isistan.archassis.ia.api;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ar.edu.unicen.exa.isistan.archassis.ia.api.db.JavaClass;
import ar.edu.unicen.exa.isistan.archassis.ia.api.impl.MySummaryData;
import ar.edu.unicen.exa.isistan.archassis.ia.comparator.AIItemResultSortByCumplido;

public class JavaClassCollector {
	private float minimoDefinido = 0.2f;
	private float medioDefinido = 0.5f;
	
	private Map<JavaClass, JavaClassEvalResult> javaClassEvalResultList;
	
	public JavaClassCollector(float minimoDefinido, float medioDefinido) {
		this.minimoDefinido = minimoDefinido;
		this.medioDefinido = medioDefinido;
		this.javaClassEvalResultList = new HashMap<JavaClass, JavaClassEvalResult>();
	}

	public JavaClassEvalResult get(JavaClass javaClass) {
		return this.javaClassEvalResultList.get(javaClass);
	}

	public void put(JavaClass javaClass, JavaClassEvalResult javaClassEvalResult) {
		this.javaClassEvalResultList.put(javaClass, javaClassEvalResult);
	}
	private List<ISummaryData> result = null;
    private List<ISummaryData> resultMedio = null;
    private List<ISummaryData> resultNoCumple = null;
    
	/**
	 * @return the result
	 */
	public List<ISummaryData> getResult() {
		compile();
		return result;
	}

	/**
	 * @return the resultMedio
	 */
	public List<ISummaryData> getResultMedio() {
		compile();
		return resultMedio;
	}

	/**
	 * @return the resultNoCumple
	 */
	public List<ISummaryData> getResultNoCumple() {
		compile();
		return resultNoCumple;
	}

	private void compile() {
		if (result != null) {return;}
		
		result = new ArrayList<ISummaryData>();
		resultMedio = new ArrayList<ISummaryData>();
	    resultNoCumple = new ArrayList<ISummaryData>();
	    
		for (JavaClassEvalResult javaClassEvalResult: javaClassEvalResultList.values()) {
			MySummaryData mySummaryData = new MySummaryData(javaClassEvalResult);
			
			//result.add(mySummaryData);
			if (!mySummaryData.getListCase().isEmpty()){
				float cumplido = mySummaryData.getListCase().get(0).getCumplido();
				if (cumplido < minimoDefinido) {
					resultNoCumple.add(mySummaryData);
				} else if (cumplido < medioDefinido) {
					resultMedio.add(mySummaryData);
				} else {
					result.add(mySummaryData);
				}
			}
		}
		Collections.sort(result, new AIItemResultSortByCumplido());
		Collections.sort(resultMedio, new AIItemResultSortByCumplido());
		Collections.sort(resultNoCumple, new AIItemResultSortByCumplido());
	}
	
	public float getPercentageResultNoCumple(){
		return this.calculatePercentage(getResultNoCumple());
	}
	
	public float getPercentageResultMedio(){
		return this.calculatePercentage(getResultMedio());
	}
	
	public float getPercentageResult(){
		return this.calculatePercentage(getResult());
	}
	
	public float getInversePercentage(){
		return ((float)javaClassEvalResultList.size())/100f;
	}
	
	private float calculatePercentage(List<ISummaryData> list){
		if (!javaClassEvalResultList.isEmpty() && !list.isEmpty())
			return (((float)list.size()) * 100f) / ((float)javaClassEvalResultList.size());
		return 0;
	}
	
}
